/*
 * @lc app=leetcode.cn id=461 lang=javascript
 *
 * [461] 汉明距离
 */

// @lc code=start
/**
 * @param {number} x
 * @param {number} y
 * @return {number}
 */
var hammingDistance = function (x, y) {
    x = x.toString(2)
    y = y.toString(2)
    let len = x.length>=y.length? x.length : y.length
    let count = 0
    while (x.length < len || y.length < len) {
        if (x.length < len) {
            x = 0 + x
        } if (y.length < len) {
            y = 0 + y
        }
    }
    for (let i = 0; i < len; i++) {
        if(x[i]!==y[i])count++
    }
    return count
};
// @lc code=end

var hammingDistance = function (x, y) {
    let count = 0
    while (x || y) {
        if (x % 2 !== y % 2) { count++ }
        x = x == 0 ? x : x >> 1
        y = y == 0 ? y : y >> 1
    }
    return count
};//位操作符解决问题，并且一直比较除以2的余数，不相等count++,直到x,y都为0
var hammingDistance = function(x, y) {
    let v = x ^ y; // 异或：相同位为0，不同位为1
    let dis = 0;
    while (v) {
        v = v & (v - 1);
        ++dis;
    }
    return dis;
};
